package com.mes.web.controller.cb;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mes.entity.cb.Materiel;
import com.mes.entity.cb.WarehouRed;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.mes.entity.cb.OrderDetailed;
import com.mes.entity.cb.Pages;
import com.mes.entity.lc.MaterialComeku;
import com.mes.entity.tdd.ProcurementRuku;
import com.mes.service.cb.ChartService;
import com.mes.utils.CustomUtils;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.EasyUIResult;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * @ClassName:ChartController
 * @Description:采购图表
 * @Author cb
 * @Date 2018年3月13日下午3:36:27
 * @version 1.0
 */
@Controller
@RequestMapping("/chart")
public class ChartController {
	
	@Resource
	private ChartService chartService;
	
	/**
	 * 图表的查询
	 * @param response
	 * @param atart_time 开始时间
	 * @param end_time 结束时间
	 * @param limit 大小
	 * @param offset 每页总数
	 * @throws Exception
	 */
	@RequestMapping("/chart_list")
	public void chart_list(HttpServletRequest request ,HttpServletResponse response,String atart_time,String end_time,String limit,String offset) throws Exception {
		Pages pages = CustomUtils.pagings(limit, offset);
		if (StringUtil.isNotEmpty(atart_time) && StringUtil.isNotEmpty(end_time)) {
			pages.setAtartTime(atart_time);
			pages.setEndTime(end_time);
		}else {
			pages.setAtartTime("2000-01-01");
			pages.setEndTime("2050-01-01");
		}
		
		List<WarehouRed> comekus = chartService.chart_list(pages);
		int total= chartService.order_total(pages);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(comekus, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 入库统计详细
	 * @param request
	 * @param response
	 * @param odd
	 * @param limit
	 * @param offset
	 */
	@RequestMapping("/chart_detal_list")
	public void chart_detal_list(HttpServletRequest request ,HttpServletResponse response,String odd,String limit,String offset) {
		System.out.println(odd);
		
	}
	
	@RequestMapping("/purchase_list")
	public void Purchase_list(HttpServletRequest request ,HttpServletResponse response,String atart_time,String end_time,String limit,String offset) throws Exception {
		Pages pages = CustomUtils.pagings(limit, offset);
		List<ProcurementRuku> list = chartService.purchase_list(pages);
		int total = chartService.getTotal(pages);
		EasyUIResult result = new EasyUIResult(total, list);
		ResponseUtil.write(response, new Gson().toJson(result));
	}
	
	/**
	 * 采购详细
	 * @param request
	 * @param response
	 * @param num  采购编号
	 * @param limit
	 * @param offset
	 * @throws Exception 
	 */
	@RequestMapping("/purchase_detal_list")
	public void purchase_detal_list(HttpServletRequest request ,HttpServletResponse response,String num,String limit,String offset) throws Exception {
		Pages pages = CustomUtils.pagings(limit, offset);
		pages.setNumber(num);
		List<Materiel> list =  chartService.purchase_detal_list(pages);
		EasyUIResult result = new EasyUIResult(list.size(), list);
		ResponseUtil.write(response, new Gson().toJson(result));
	}


	
}
